package com.ssyc.gsk_teacher.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.bean.RowIdInfo;
import com.ssyc.gsk_teacher.bean.TeacherShowMineInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 教师风采个人视频列表适配器
 * Created by Kevin on 2018/6/29
 */

public class TeacherShowMineAdapter extends BaseQuickAdapter<TeacherShowMineInfo.ListBean, BaseViewHolder> {
    private Context context;
    private List<TeacherShowMineInfo.ListBean> data = new ArrayList<>();
    private List<RowIdInfo> json = new ArrayList<>();
    private boolean falg = false;


    public TeacherShowMineAdapter(Context context, int layoutResId, @Nullable List<TeacherShowMineInfo.ListBean> data) {
        super(layoutResId, data);
        this.context = context;
        this.data = data;
    }

    //编辑
    public void Edit(boolean flag) {
        this.falg = flag;
        this.notifyDataSetChanged();
    }

    //全选
    public void selectALL(boolean isChecked) {
        for (int i = 0; i < data.size(); i++) {
            if (isChecked) {
                data.get(i).setSelected(true);
            } else {
                data.get(i).setSelected(false);
            }
        }
        this.notifyDataSetChanged();
    }


    //获取选中的条目id
    public String getJson() {
        String dataJson = "";
        json.clear();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getSelected()) {
                RowIdInfo rid = new RowIdInfo();
                rid.rowId = getItem(i).getRowId()+"";
                json.add(rid);
            }
        }
        dataJson = GsonUtil.listToJson(json);
        return dataJson;
    }

    @Override
    protected void convert(BaseViewHolder helper, TeacherShowMineInfo.ListBean item) {
        //条目外层布局
        LinearLayout llOut = helper.getView(R.id.ll_out);
        //是否选中的图标
        ImageView iv_is_sel = helper.getView(R.id.iv_is_sel);
        //视频图片
        ImageView iv_vidio_img = helper.getView(R.id.iv_vidio_img);
        //视频名称
        TextView tv_vidio_name = helper.getView(R.id.tv_vidio_name);
        //视频日期
        TextView tv_vidio_date = helper.getView(R.id.tv_vidio_date);

        //标题名称
        tv_vidio_name.setText(item.getTitle());
        //创建日期
        tv_vidio_date.setText(item.getCreateTime());

        //图片
        ImageUtil.displayImage(context,iv_vidio_img,item.getPictureUrl(),
                R.drawable.teacher_show_vidio_default);

        if (item.getSelected()) {
            iv_is_sel.setImageResource(R.drawable.teacher_show_sel);
        } else {
            iv_is_sel.setImageResource(R.drawable.teacher_show_nor);
        }

        if (falg) {
            //编辑状态
            iv_is_sel.setVisibility(View.VISIBLE);
        } else {
            //非编辑状态
            iv_is_sel.setVisibility(View.GONE);
        }

        //整体条目点击事件
        llOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (falg) {
                    if (item.getSelected()) {
                        item.setSelected(false);
                    } else {
                        item.setSelected(true);
                    }
                }
                notifyDataSetChanged();
                Log.i("test", "d点了" );
            }
        });


    }
}
